package com.uyaer.wheel.display.scene
{
	import com.uyaer.wheel.common.Common;
	import com.uyaer.wheel.events.EventType;
	import com.uyaer.wheel.menu.MenuType;
	import com.uyaer.wheel.res.ResManager;
	import com.uyaer.wheel.story.Story;
	
	import starling.animation.Transitions;
	import starling.core.Starling;
	import starling.display.Button;
	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.events.Event;

	/**
	 * 暂停界面
	 * @author hc
	 * 
	 */	
	public class Pause extends Sprite implements IScene
	{

		private var resumeBtn:Button;
		private var backHomeBtn:Button;

		private var replayBtn:Button;
		
		public function Pause()
		{
			if(stage){
				onAdded();
			}else{
				this.addEventListener(Event.ADDED_TO_STAGE,onAdded);
			}
		}
		
		private function onAdded(e:Event = null):void{
			this.removeEventListener(Event.ADDED_TO_STAGE,onAdded);
			
			this.layout();
			this.flatten();
		}
		
		
		/**
		 *布局 
		 */		
		private function layout():void{
			var q:Quad = new Quad(Common.W,Common.H,0x000000);
			q.alpha = 0.85;
			q.touchable = false;
			addChild(q);
			
			resumeBtn = new  Button(ResManager.me.getTexture("menu_resume"));
			replayBtn = new  Button(ResManager.me.getTexture("menu_replay"));
			backHomeBtn = new Button(ResManager.me.getTexture("menu_back_home"));
			replayBtn.scaleX = replayBtn.scaleY = resumeBtn.scaleX = resumeBtn.scaleY = backHomeBtn.scaleX = backHomeBtn.scaleY = Common.S;
			replayBtn.x = resumeBtn.x = backHomeBtn.x = 0.25 * Common.W;
			resumeBtn.y = Common.H * 0.32;
			replayBtn.y = Common.H * 0.42;
			backHomeBtn.y = Common.H * 0.58;
			addChild(resumeBtn);
			addChild(replayBtn);
			addChild(backHomeBtn);
		}
		
		
		public function start():void{
			this.y = Common.H;
			Starling.juggler.tween(this,0.5,{"y":0,"transition":Transitions.EASE_OUT,
						"onComplete":rigiest});
		}
		
		private function rigiest():void{
			this.addEventListener(Event.TRIGGERED,onTriggered);
		}
		
		private function onTriggered(e:Event):void{
			switch(e.target){
				case resumeBtn:
					stage.dispatchEventWith(EventType.MENU_SELECT,false,MenuType.RESUME_GAME);
					break;
				case replayBtn:
					Story.instance.showAlert("确定放弃游戏,重新开始？",function():void{
						stage.dispatchEventWith(EventType.MENU_SELECT,false,MenuType.REPLAY_GAME);
					});
					break;
				case backHomeBtn:
					Story.instance.showAlert("确定放弃游戏,回到主界面？",function():void{
						stage.dispatchEventWith(EventType.MENU_SELECT,false,MenuType.SHOW_HOME);
					});
					break;
			}
		}
		
		public function stop():void{
			this.removeEventListener(Event.TRIGGERED,onTriggered);
		}
	}
}